home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK1.toast / Development Kits / Mac OS / Display Manager SDK / Sample Code / Display Changed CWPro4 / Source / windows.c < prev   
Encoding:
C/C++ Source or Header  |  1999-01-20  |  11.2 KB  |  491 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        Windows.c
  4. #
  5. #        This segment handles the window creation, close, updates,
  6. #
  7. #        Author(s):     Michael Marinkovich
  8. #                    marink@apple.com
  9. #
  10. #        Modification History: 
  11. #
  12. #            1/19/99        ewa     update to CWPro3 and universal interfaces                     
  13. #            10/12/95    MWM        cleaned up
  14. #            6/4/95        MWM     Initial coding                     
  15. #
  16. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  17. #
  18. #
  19. #        You may incorporate this sample code into your applications without
  20. #        restriction, though the sample code has been provided "AS IS" and the
  21. #        responsibility for its operation is 100% yours.  However, what you are
  22. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  23. #        after having made changes. If you're going to re-distribute the source,
  24. #        we require that you make it clear in the source that the code was
  25. #        descended from Apple Sample Code, but that you've made changes.
  26. #
  27. *************************************************************************************/
  28.  
  29. #include <Windows.h>
  30.  
  31. #include "App.h"
  32. #include "Proto.h"
  33.  
  34.  
  35. //----------------------------------------------------------------------
  36. //    Globals - 
  37. //----------------------------------------------------------------------
  38.  
  39. extern Boolean        gHasAbout;        // have an about box?
  40. extern short        gWindCount;
  41.  
  42.  
  43. //----------------------------------------------------------------------
  44. //
  45. //    CreateWindow - create a window from the info passed in. Will try to 
  46. //                   load from resource if resID is supplied.
  47. //
  48. //----------------------------------------------------------------------
  49.  
  50. WindowPtr CreateWindow(short resID, void *wStorage, Rect *bounds,  Str255 title,
  51.                         Boolean visible, short procID , short kind,
  52.                         WindowRef behind, Boolean goAwayFlag, long refCon)
  53. {
  54.     OSErr            err = nil;
  55.     WindowRef        newWindow = nil;
  56.     
  57.     
  58.     if (resID != nil)         // if res id isn't nil then load from disk
  59.         newWindow = GetNewWindow(resID, wStorage, behind);
  60.     else                    // otherwise make a new windowRecord
  61.          newWindow = NewWindow(wStorage, bounds, title, visible, 
  62.                                procID, behind, goAwayFlag, refCon);
  63.                                
  64.     if (newWindow != nil) 
  65.     {
  66.         NewWindowTitle(newWindow, title);
  67.         err = InitWindowProcs(newWindow, kind);
  68.         
  69.         if (err == noErr) 
  70.         {
  71.             SetPort(newWindow);
  72.             ShowWindow(newWindow);
  73.         }
  74.                 
  75.         // initialization of Document Record faild
  76.         // so kill the return window    
  77.         else 
  78.         {
  79.             newWindow = nil;
  80.             HandleError(err, false);
  81.         }        
  82.     }
  83.     
  84.     return newWindow;
  85.  
  86. }
  87.     
  88.  
  89. //----------------------------------------------------------------------
  90. //
  91. //    RemoveWindow - applications Doc window disposal routine.
  92. //                 
  93. //
  94. //----------------------------------------------------------------------
  95.  
  96. OSErr RemoveWindow(WindowRef window)
  97. {
  98.     OSErr            err = nil;
  99.     short            kind;
  100.     DocHnd            doc;
  101.     
  102.     
  103.     kind = GetWindKind(window);
  104.     if (kind < kDocKind || kind > kAboutKind)
  105.         return -1; // not our window
  106.     
  107.     doc = (DocHnd)GetWRefCon(window);
  108.     if (doc != nil) 
  109.     {
  110.         switch (kind) 
  111.         {
  112.             case kDocKind:
  113.                 DisposeGWorld((**doc).world);
  114.                 DisposeHandle((Handle)doc);
  115.                 DisposeWindow(window);
  116.                 err = noErr;
  117.                 break;
  118.             
  119.             case kAboutKind:
  120.                 DisposeHandle((Handle)doc);
  121.                 DisposeWindow(window);
  122.                 err = noErr;
  123.                 gHasAbout = false;    // allow new about box
  124.                 break;
  125.             
  126.             default:
  127.                 err = paramErr;
  128.                 break;
  129.                     
  130.  
  131.         }    
  132.     }
  133.     
  134.     return err;
  135.     
  136. }        
  137.  
  138.  
  139. //----------------------------------------------------------------------
  140. //
  141. //    NewWindowTitle - if supplied title is nil then title is set to 
  142. //                      global "gWindCount".
  143. //
  144. //----------------------------------------------------------------------
  145.  
  146. void NewWindowTitle(WindowRef window, Str255 str)
  147. {
  148.     Str255        catStr = "\pUntitled ";
  149.     Str255        newStr;
  150.     
  151.     
  152.     if (str == nil || StrLength(str) == 0) 
  153.     {
  154.         pstrcpy(newStr, catStr);
  155.         NumToString(gWindCount, catStr);
  156.         pstrcat(newStr, catStr);
  157.         gWindCount++;
  158.         
  159.         SetWTitle(window,newStr);
  160.     }
  161.     else
  162.         SetWTitle(window, str);
  163.  
  164. }
  165.  
  166.  
  167. //----------------------------------------------------------------------
  168. //
  169. //    InitWindowProcs - init a window with proper callback event. fills 
  170. //                       out custom procs for different windowkinds.
  171. //
  172. //----------------------------------------------------------------------
  173.  
  174. OSErr InitWindowProcs(WindowRef window, short windKind)
  175. {
  176.     OSErr            err = nil;
  177.     DocHnd            doc;
  178.     
  179.     // just in case
  180.     if (nil == window)
  181.         return noErr;
  182.         
  183.     doc = (DocHnd)NewHandle(sizeof(DocRec));
  184.     if (doc != nil) 
  185.     {
  186.         SetWRefCon(window, (long)doc);
  187.  
  188.         switch(windKind) 
  189.         {
  190.             case kDocKind:
  191.                 (**doc).idleProc        = DoIdle;
  192.                 (**doc).mMenuProc        = HandleMenuChoice;
  193.                 (**doc).inContentProc    = HandleContentClick;
  194.                 (**doc).inGoAwayProc    = nil;
  195.                 (**doc).inZoomProc        = HandleZoomClick;
  196.                 (**doc).inGrowProc        = HandleGrow;
  197.                 (**doc).keyProc            = nil;
  198.                 (**doc).activateProc    = DoActivate;
  199.                 (**doc).updateProc        = DrawWindow;    
  200.                 (**doc).hScroll         = nil;
  201.                 (**doc).vScroll            = nil;
  202.                 (**doc).world            = nil;
  203.                 (**doc).pict            = nil;
  204.                 (**doc).printer            = nil;
  205.                 (**doc).dirty            = false;
  206.                 
  207.                 InstallScrollBars(window, doc);
  208.  
  209.                 break;
  210.                 
  211.             case kDialogKind:
  212.                 break;
  213.  
  214.             case kAboutKind:
  215.                 (**doc).idleProc        = DoIdle;
  216.                 (**doc).mMenuProc        = HandleMenuChoice;
  217.                 (**doc).inContentProc    = nil;
  218.                 (**doc).inGoAwayProc    = nil;
  219.                 (**doc).inZoomProc        = nil;
  220.                 (**doc).inGrowProc        = nil;
  221.                 (**doc).keyProc            = nil;
  222.                 (**doc).activateProc    = nil;
  223.                 (**doc).updateProc        = DrawAboutWindow;    
  224.                 (**doc).hScroll         = nil;
  225.                 (**doc).vScroll            = nil;
  226.                 (**doc).world            = nil;
  227.                 (**doc).pict            = GetPicture(rAboutPictID);
  228.                 (**doc).printer            = nil;
  229.                 (**doc).dirty            = false;
  230.                 
  231.                 break;
  232.                 
  233.             default:
  234.                 err = 25;
  235.                 break;    
  236.         }
  237.         ((WindowPeek)window)->windowKind = windKind;
  238.  
  239.     }            
  240.     return err;
  241.  
  242. }
  243.  
  244.  
  245. //----------------------------------------------------------------------
  246. //
  247. //    PictToWorld - create a GWorld from a Pict. Size is determined by the
  248. //                  bounds of the pict. If the pict is nil then a default
  249. //                  bounds is used, in case of an empty window.
  250. //----------------------------------------------------------------------
  251.     
  252. GWorldPtr PictToWorld(PicHandle pict, OSErr *rtnErr)
  253. {
  254.     OSErr            err;
  255.     GWorldPtr        oldWorld;
  256.     GWorldPtr        theWorld = nil;
  257.     GDHandle        oldGD;
  258.     PixMapHandle    thePix;
  259.     Rect            bounds;
  260.     
  261.     GetGWorld(&oldWorld, &oldGD);
  262.     
  263.     if (pict != nil)
  264.         bounds = (**pict).picFrame;
  265.     else
  266.         SetRect(&bounds, 0, 0, 200, 200);    
  267.     
  268.     err = NewGWorld(&theWorld, 8,&bounds, nil, nil, 0L);
  269.     if (err == noErr && theWorld != nil) 
  270.     {
  271.         thePix = GetGWorldPixMap(theWorld);
  272.         if (LockPixels(thePix)) 
  273.         {
  274.             SetGWorld(theWorld, nil);
  275.             EraseRect(&bounds);
  276.             if (pict != nil)
  277.                 DrawPicture(pict, &bounds);
  278.             
  279.             UnlockPixels(thePix);
  280.         }
  281.         SetGWorld(oldWorld, oldGD);
  282.     }
  283.     
  284.     *rtnErr = err;
  285.     
  286.     return theWorld;
  287. }
  288.  
  289.         
  290. //----------------------------------------------------------------------
  291. //
  292. //    DrawWindow - custom proc that is called to update window contents.
  293. //                 
  294. //
  295. //----------------------------------------------------------------------
  296.  
  297. void DrawWindow(WindowRef window, void *refCon)
  298. {
  299.     DocHnd            doc;
  300.     GWorldPtr        theWorld;
  301.     PixMapHandle    thePix;
  302.     Rect            cRect;
  303.     Rect            bounds;
  304.     
  305.     doc = (DocHnd)GetWRefCon(window);    
  306.     if (doc != nil) 
  307.     {
  308.         SetPort(window);
  309.         GetContRect(window, &cRect);
  310.         ClipRect(&cRect);
  311.         
  312.         theWorld = (**doc).world;
  313.  
  314.         if (theWorld != nil) 
  315.         {
  316.             bounds = theWorld->portRect;
  317.             OffsetRect(&bounds, -GetControlValue((**doc).hScroll), 
  318.                        -GetControlValue((**doc).vScroll));
  319.                        
  320.             thePix = GetGWorldPixMap(theWorld);
  321.             if (LockPixels(thePix)) 
  322.             {
  323.                 CopyBits((BitMap *) *thePix, &window->portBits,
  324.                          &theWorld->portRect, &bounds, srcCopy, nil);
  325.                 
  326.                 UnlockPixels(thePix);
  327.             }             
  328.                         
  329.         }
  330.         else
  331.             EraseRect(&cRect);    
  332.  
  333.         ClipRect(&window->portRect);
  334.         DrawGrowIcon(window);
  335.         UpdateControls(window, window->visRgn);
  336.     }
  337.  
  338. }
  339.  
  340.  
  341. //----------------------------------------------------------------------
  342. //
  343. //    DrawAboutWindow - custom proc that is called to update about window.
  344. //                 
  345. //
  346. //----------------------------------------------------------------------
  347.  
  348. void DrawAboutWindow(WindowRef window, void *refCon)
  349. {    
  350.     DocHnd        doc;
  351.     
  352.     doc = (DocHnd)GetWRefCon(window);    
  353.     if ((**doc).pict != nil) 
  354.         DrawPicture((**doc).pict, &window->portRect);
  355.  
  356. }
  357.  
  358.  
  359. //----------------------------------------------------------------------
  360. //
  361. //    DoResizeWindow - custom proc that is called to update window.
  362. //                 
  363. //
  364. //----------------------------------------------------------------------
  365.  
  366. void DoResizeWindow (WindowRef window) 
  367. {
  368.     DocHnd                doc;
  369.     ControlHandle        hCtl, vCtl;
  370.     RgnHandle            tempRgn;
  371.     RgnHandle            oldCtlRgn;
  372.     Rect                hSRect;
  373.     Rect                newDocRect;
  374.     Rect                pictRect;
  375.     Rect                paneRect;
  376.     short                max, oldCntlVal;
  377.     short                pV, wV;
  378.     short                 pH, wH;
  379.  
  380.     doc = (DocHnd)GetWRefCon(window);
  381.     if (doc != nil) 
  382.     {
  383.         pictRect = (**doc).world->portRect;
  384.         newDocRect = (**window->visRgn).rgnBBox;
  385.  
  386.         hCtl = (**doc).hScroll;
  387.         vCtl = (**doc).vScroll;
  388.         
  389.         hSRect = (**hCtl).contrlRect;
  390.         hSRect.right += kScrollWidth;
  391.         ClipRect(&window->portRect);
  392.         RectRgn(oldCtlRgn = NewRgn(), &hSRect);
  393.         RectRgn(tempRgn = NewRgn(), &(**vCtl).contrlRect);
  394.         UnionRgn(oldCtlRgn, tempRgn, oldCtlRgn);
  395.         EraseRgn(oldCtlRgn);
  396.         
  397.         (**hCtl).contrlVis = 0;
  398.         (**vCtl).contrlVis = 0;
  399.  
  400.         paneRect = newDocRect;
  401.         paneRect.bottom -= kScrollWidth;
  402.         paneRect.right -= kScrollWidth;
  403.         
  404.         MoveControl(hCtl, paneRect.left - 1, paneRect.bottom);    
  405.         MoveControl(vCtl, paneRect.right, paneRect.top - 1);
  406.             
  407.         SizeControl(hCtl, 2+paneRect.right - paneRect.left, kScrollWidth + 1);    
  408.         SizeControl(vCtl, kScrollWidth + 1, 2+paneRect.bottom - paneRect.top);
  409.         
  410.         (**hCtl).contrlVis = 255;
  411.         (**vCtl).contrlVis = 255;
  412.         
  413.         pV = pictRect.bottom - pictRect.top;
  414.         wV = paneRect.bottom - paneRect.top;
  415.         if (wV >= pV)
  416.             max = 0;
  417.         else
  418.             max = ((MAX (0, pV - wV)) / kScrollDelta) + 1;
  419.             
  420.         oldCntlVal = GetControlValue(vCtl);
  421.         SetControlMinimum(vCtl, 0);
  422.         SetControlMaximum(vCtl, max);
  423.         SetControlValue(vCtl, oldCntlVal );
  424.         
  425.         HiliteControl(vCtl, (max == 0) ? (255):(0));    
  426.  
  427.         pH = pictRect.right - pictRect.left;
  428.         wH = paneRect.right - paneRect.left;
  429.         if (wH >= pH)
  430.             max = 0 ;
  431.         else
  432.             max = ((MAX (0, pH - wH )) / kScrollDelta) +1;
  433.         oldCntlVal = GetControlValue(hCtl);
  434.         SetControlMinimum(hCtl, 0);
  435.         SetControlMaximum(hCtl, max) ;
  436.         SetControlValue(hCtl, oldCntlVal);
  437.         
  438.         HiliteControl(hCtl, (max == 0) ? (255):(0));    
  439.                 
  440.         DisposeRgn(oldCtlRgn);
  441.         DisposeRgn(tempRgn);
  442.         AdjustScrollValues(window);
  443.         InvalRect(&window->portRect);
  444.  
  445.     }
  446.  
  447. }
  448.  
  449.  
  450. //----------------------------------------------------------------------
  451. //
  452. //    GetWindKind - returns the windowkind.
  453. //                 
  454. //
  455. //----------------------------------------------------------------------
  456.  
  457. short GetWindKind(WindowRef window)
  458. {
  459.  
  460.     return ((WindowPeek)window)->windowKind;
  461.  
  462. }
  463.  
  464.  
  465. //----------------------------------------------------------------------
  466. //
  467. //    GetIsAppWindow - is the window a 'userKind'.
  468. //                 
  469. //
  470. //----------------------------------------------------------------------
  471.  
  472. Boolean GetIsAppWindow(WindowRef window)
  473. {
  474.     return (GetWindKind(window) == kDocKind);
  475.  
  476. }
  477.  
  478.  
  479. //----------------------------------------------------------------------
  480. //
  481. //    GetIsAboutWindow - is the window an about box.
  482. //                 
  483. //
  484. //----------------------------------------------------------------------
  485.  
  486. Boolean GetIsAboutWindow(WindowRef window)
  487. {
  488.     return (GetWindKind(window) == kAboutKind);
  489.     
  490. }
  491.